package action.plan;

import persistence.model.TdfkfaMain;
import persistence.model.TdfkfaPrjExperts;
import presentation.extendmodel.TdfkfaPrjExpertsEx;
import service.PlanService;

import com.opensymphony.xwork2.ActionSupport;

import constant.DictConstant;
import constant.ServiceConstants;

public class AssignExpertAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private PlanService planService;

    private String planId;

    private TdfkfaPrjExperts prjExperts;

    private TdfkfaPrjExpertsEx assignedExperts;

    private String status;

    public String assignExpertPre() throws Exception {
        return SUCCESS;
    }

    public String assignExpert() throws Exception {
        int ret = planService.assignExpert(prjExperts);

        if (ret == ServiceConstants.FAILURE) {
            status = "failure";
        }

        TdfkfaMain main = planService.getPlanMain(prjExperts.getPrjPkid());
        String sczt = main.getPrjScztcode().toString();
        if (DictConstant.SCZT_FIRST_NO_COMMIT.equals(sczt)) {
        	sczt = DictConstant.SCZT_FIRST_COMMITTED;
        } else if (DictConstant.SCZT_SECOND_NO_COMMIT.equals(sczt)) {
        	sczt = DictConstant.SCZT_SECOND_COMMITTED;
        } else if (DictConstant.SCZT_THIRD_NO_COMMIT.equals(sczt)) {
        	sczt = DictConstant.SCZT_THIRD_COMMITTED;
        } else {
        	sczt = null;
        }
        if (ret == ServiceConstants.SUCCESS) {
        	int ret2 = planService.setPlanState(prjExperts.getPrjPkid(), sczt, DictConstant.FAZT_JUDGING);
        	if (ret2 == ServiceConstants.SUCCESS) {
        		status = "success";
        	} else {
        		status = "success";
        	}
        }
        return SUCCESS;
    }

    public String loadAssignedExpert() throws Exception {
        assignedExperts = planService.getAssignedExpert(planId);
        return SUCCESS;
    }

    public void setPlanService(PlanService planService) {
        this.planService = planService;
    }

    public String getPlanId() {
        return planId;
    }

    public void setPlanId(String planId) {
        this.planId = planId;
    }

    public void setPrjExperts(TdfkfaPrjExperts prjExperts) {
        this.prjExperts = prjExperts;
    }

    public TdfkfaPrjExperts getPrjExperts() {
        return prjExperts;
    }

    public TdfkfaPrjExpertsEx getAssignedExperts() {
        return assignedExperts;
    }

    public String getStatus() {
        return status;
    }

}
